package com.bicon.botu.medical.service.impl

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.baomidou.mybatisplus.mapper.EntityWrapper
import com.baomidou.mybatisplus.service.impl.ServiceImpl
import com.bicon.botu.core.base.common.Constants
import com.bicon.botu.core.base.common.utils.IdGenerator
import com.bicon.botu.core.base.common.utils.StringUtils
import com.bicon.botu.core.base.repository.redis.RedisHandler
import com.bicon.botu.medical.common.constants.PersonConstants
import com.bicon.botu.medical.dao.mapper.UserMapper
import com.bicon.botu.medical.model.dto.UserInfoParam
import com.bicon.botu.medical.model.entity.Account
import com.bicon.botu.medical.model.entity.IdentifyingCode
import com.bicon.botu.medical.model.entity.User
import com.bicon.botu.medical.service.IUserService
import com.bicon.botu.third.provider.rpc.service.MessageRpcService
import com.bicon.botu.third.service.MessageService
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*

@Service
open class UserServiceImpl : ServiceImpl<UserMapper,User>(), IUserService {

    /**
     * 第三方服务，短信发送
     */
    @Autowired
    internal var thirdService: MessageRpcService? = null

    @Autowired
    internal var messageService: MessageService? = null

    @Autowired
    private val redisHandler: RedisHandler? = null



    override fun getPersonData(userId: String):User{
        return User().selectOne(EntityWrapper<User>().eq("id", userId).eq("logic_delete", Constants.DEL_LOGIC_NORMAL))
    }

    override fun updatePersonData(userInfoParam: UserInfoParam): Boolean {
        val user = User()
        user.preUpdate()
        BeanUtils.copyProperties(userInfoParam, user)
        val falg = user.updateById()
        return  falg;
    }

    /**
     * 发送短信验证码
     *
     * @param phone 手机号
     * @return true发送成功、false 发送失败
     */
    override fun sendSmsCode(phone: String, type: Int?): Boolean {
        //六位随机码
        val code = IdGenerator.createXCode(6)
        //第三方服务发送
        val resultJson = thirdService!!.sendYunSms(phone, code, null)
        if (resultJson != null) {
            if (resultJson.code === Constants.RESPONSE_CODE_200) {
                //发送成功 需要缓存到redis
                var identifyingCode = IdentifyingCode()
                val sendCode = redisHandler!!.getGlobalRedisTemplate().getStringValue(PersonConstants.REDISPREFIX_CODE, phone + type!!.toString())
                if (!StringUtils.isEmpty(sendCode)) {
                    identifyingCode = JSONObject.parseObject<IdentifyingCode>(sendCode, IdentifyingCode::class.java)
                } else {
                    identifyingCode.count
                }
                val date = Date()
                identifyingCode.account=phone
                identifyingCode.code = code
                identifyingCode.createTime = date
                identifyingCode.updateTime =date
                identifyingCode.count = identifyingCode.count!!.plus(1)
                redisHandler.getGlobalRedisTemplate().cacheStringValue(PersonConstants.REDISPREFIX_CODE, phone + type.toString(), JSON.toJSONString(identifyingCode), PersonConstants.CODE_EXPIRE_TIME)
                return true
            }
            //失败
            return false
        }
        //失败
        return false
    }

    /**
     * 生产验证码并返回，测试用
     *
     * @param phone
     * @param type
     * @return
     */
    override fun sendSmsCodeString(phone: String, type: Int?): String {
        //六位随机码
        val code = IdGenerator.createXCode(6)
        //发送成功 需要缓存到redis
        var identifyingCode = IdentifyingCode()
        val sendCode = redisHandler!!.getGlobalRedisTemplate().getStringValue(PersonConstants.REDISPREFIX_CODE, phone + type!!.toString())
        if (!StringUtils.isEmpty(sendCode)) {
            identifyingCode = JSONObject.parseObject<IdentifyingCode>(sendCode, IdentifyingCode::class.java)
        } else {
            identifyingCode.count= 0
        }
        val date = Date()
        identifyingCode.account=phone
        identifyingCode.code = code
        identifyingCode.createTime = date
        identifyingCode.updateTime =date
        identifyingCode.count = identifyingCode.count!!.plus(1)
        redisHandler.getGlobalRedisTemplate().cacheStringValue(PersonConstants.REDISPREFIX_CODE, phone + type.toString(), JSON.toJSONString(identifyingCode), PersonConstants.CODE_EXPIRE_TIME)
        return code
    }

    /**
     * 校验验证码
     *
     * @param phone 手机号
     * @param code  验证码
     * @return true验证成功、false 验证失败
     */
    override fun verifySmsCode(phone: String, code: String, type: Int?): Boolean {
        //根据缓存获取验证码
        val sendCode = redisHandler!!.getGlobalRedisTemplate().getStringValue(PersonConstants.REDISPREFIX_CODE, phone + type!!.toString())
        if (StringUtils.isEmpty(sendCode)) {
            return false
        }
        val identifyingCode = JSONObject.parseObject<IdentifyingCode>(sendCode, IdentifyingCode::class.java)
        return if (identifyingCode.code != code) {
            false
        } else {
            true
        }
    }

    override fun forgetPassByAccout(account: String, password: String, type: Int?): Boolean {
        val account = Account()
        account.password = password
        account.preUpdate()
        return account.update(EntityWrapper<Account>().eq("account", account).eq("type", type))
    }
}
